<template>
  <div class="collection-record">
    <div class="page-header">
      <h1>收款记录</h1>
      <div class="breadcrumb">财务管理 / 收款记录</div>
    </div>

    <div class="search-section">
      <div class="search-form">
        <div class="form-item">
          <label>收款编号</label>
          <input v-model="searchForm.collectionCode" type="text" placeholder="请输入收款编号" />
        </div>
        <div class="form-item">
          <label>客户</label>
          <input v-model="searchForm.customer" type="text" placeholder="请输入客户名称" />
        </div>
        <div class="form-item">
          <label>收款日期</label>
          <input v-model="searchForm.collectionDate" type="date" />
        </div>
        <div class="form-buttons">
          <button class="btn btn-primary" @click="searchRecords">查询</button>
          <button class="btn btn-secondary" @click="resetSearch">重置</button>
        </div>
      </div>
    </div>

    <div class="table-pagination-section">
      <div class="table-container">
        <table class="data-table">
          <thead>
            <tr>
              <th>收款编号</th>
              <th>客户</th>
              <th>收款金额</th>
              <th>收款方式</th>
              <th>收款日期</th>
              <th>备注</th>
              <th>创建人</th>
              <th>创建时间</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="record in recordList" :key="record.id">
              <td>{{ record.collectionCode }}</td>
              <td>{{ record.customerName }}</td>
              <td>{{ record.amount }}</td>
              <td>{{ record.collectionMethod }}</td>
              <td>{{ record.collectionDate }}</td>
              <td>{{ record.remark || '-' }}</td>
              <td>{{ record.creator }}</td>
              <td>{{ record.createTime }}</td>
              <td>
                <button class="btn btn-small btn-primary" @click="viewDetail(record)">
                  <span class="btn-icon">🔍</span>
                  查看
                </button>
              </td>
            </tr>
          </tbody>
        </table>
      </div>

      <div class="pagination">
        <div class="pagination-info">共{{ totalPages }}页/{{ totalItems }}条数据</div>
        <div class="pagination-controls">
          <button class="btn btn-page" :disabled="currentPage === 1" @click="changePage(currentPage - 1)">上一页</button>
          <button v-for="page in visiblePages" :key="page" class="btn btn-page" :class="{ active: page === currentPage }" @click="changePage(page)">{{ page }}</button>
          <button class="btn btn-page" :disabled="currentPage === totalPages" @click="changePage(currentPage + 1)">下一页</button>
        </div>
        <div class="pagination-jump">
          <span>跳至</span>
          <input type="number" v-model="jumpPage" min="1" :max="totalPages" />
          <span>页</span>
          <button class="btn btn-small" @click="jumpToPage">确定</button>
        </div>
      </div>
    </div>

    <!-- 收款详情模态框 -->
    <div v-if="showDetailModal" class="modal-overlay" @click="showDetailModal = false">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>收款详情</h3>
          <button class="modal-close" @click="showDetailModal = false">&times;</button>
        </div>
        <div class="modal-body">
          <div class="detail-grid">
            <div class="detail-item">
              <label>收款编号：</label>
              <span>{{ selectedRecord?.collectionCode }}</span>
            </div>
            <div class="detail-item">
              <label>客户名称：</label>
              <span>{{ selectedRecord?.customerName }}</span>
            </div>
            <div class="detail-item">
              <label>收款金额：</label>
              <span class="highlight-amount">{{ selectedRecord?.amount }}元</span>
            </div>
            <div class="detail-item">
              <label>收款方式：</label>
              <span>{{ selectedRecord?.collectionMethod }}</span>
            </div>
            <div class="detail-item">
              <label>收款日期：</label>
              <span>{{ selectedRecord?.collectionDate }}</span>
            </div>
            <div class="detail-item">
              <label>创建人：</label>
              <span>{{ selectedRecord?.creator }}</span>
            </div>
            <div class="detail-item">
              <label>创建时间：</label>
              <span>{{ selectedRecord?.createTime }}</span>
            </div>
            <div class="detail-item">
              <label>关联订单：</label>
              <span>{{ selectedRecord?.orderNumber || '-' }}</span>
            </div>
            <div class="detail-item">
              <label>订单类型：</label>
              <span>{{ selectedRecord?.orderType || '-' }}</span>
            </div>
            <div class="detail-item full-width">
              <label>备注：</label>
              <span>{{ selectedRecord?.remark || '-' }}</span>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button class="btn btn-secondary" @click="showDetailModal = false">关闭</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { apiGet } from '../../utils/api.js'

const searchForm = reactive({
  collectionCode: '',
  customer: '',
  collectionDate: ''
})

const recordList = ref([])
const currentPage = ref(1)
const totalPages = ref(5)
const totalItems = ref(50)
const jumpPage = ref(1)
const showDetailModal = ref(false)
const selectedRecord = ref(null)

const visiblePages = computed(() => {
  const pages = []
  const start = Math.max(1, currentPage.value - 2)
  const end = Math.min(totalPages.value, currentPage.value + 2)
  for (let i = start; i <= end; i++) {
    pages.push(i)
  }
  return pages
})

const searchRecords = async () => {
  currentPage.value = 1
  await loadRecordData()
}

const resetSearch = async () => {
  searchForm.collectionCode = ''
  searchForm.customer = ''
  searchForm.collectionDate = ''
  currentPage.value = 1
  await loadRecordData()
}

const changePage = async (page) => {
  currentPage.value = page
  await loadRecordData()
}

const jumpToPage = async () => {
  if (jumpPage.value >= 1 && jumpPage.value <= totalPages.value) {
    currentPage.value = jumpPage.value
    await loadRecordData()
  }
}

const viewDetail = (record) => {
  selectedRecord.value = record
  showDetailModal.value = true
}

const loadRecordData = async () => {
  try {
    const params = new URLSearchParams({
      page: currentPage.value,
      size: 10,
      ...(searchForm.collectionCode && { collectionCode: searchForm.collectionCode }),
      ...(searchForm.customer && { customer: searchForm.customer }),
      ...(searchForm.collectionDate && { collectionDate: searchForm.collectionDate })
    })

    const data = await apiGet(`/finance/collection-records?${params}`)

    if (data.content) {
      recordList.value = data.content.map(record => ({
        ...record,
        // 确保字段映射正确
        method: record.collectionMethod || '银行转账',
        createTime: record.createTime ? record.createTime.split('T')[0] : ''
      }))
      totalPages.value = data.totalPages
      totalItems.value = data.totalElements
    } else {
      // 如果没有数据，使用模拟数据
      recordList.value = Array.from({ length: 10 }, (_, index) => ({
        id: index + 1,
        collectionCode: `SK${String(index + 1).padStart(4, '0')}`,
        customerName: 'XXX客户',
        amount: '5000.00',
        method: '银行转账',
        collectionDate: '2024-08-26',
        remark: '',
        creator: '夏油杰',
        createTime: '2024-08-26'
      }))
      totalPages.value = 5
      totalItems.value = 50
    }
  } catch (error) {
    console.error('加载收款记录失败:', error)
    // 使用模拟数据作为后备
    recordList.value = Array.from({ length: 10 }, (_, index) => ({
      id: index + 1,
      collectionCode: `SK${String(index + 1).padStart(4, '0')}`,
      customerName: 'XXX客户',
      amount: '5000.00',
      method: '银行转账',
      collectionDate: '2024-08-26',
      remark: '',
      creator: '夏油杰',
      createTime: '2024-08-26'
    }))
    totalPages.value = 5
    totalItems.value = 50
  }
}

onMounted(async () => {
  await loadRecordData()
})
</script>

<style scoped>
.collection-record {
  padding: 20px;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  position: relative;
}

.collection-record::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="25" cy="25" r="1" fill="rgba(255,255,255,0.1)"/><circle cx="75" cy="75" r="1" fill="rgba(255,255,255,0.1)"/><circle cx="50" cy="10" r="0.5" fill="rgba(255,255,255,0.1)"/><circle cx="10" cy="60" r="0.5" fill="rgba(255,255,255,0.1)"/><circle cx="90" cy="40" r="0.5" fill="rgba(255,255,255,0.1)"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
  opacity: 0.3;
  pointer-events: none;
}

.page-header {
  margin-bottom: 30px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 25px 30px;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(102, 126, 234, 0.3);
  position: relative;
  overflow: hidden;
}

.page-header::before {
  content: '';
  position: absolute;
  top: -50%;
  right: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
  animation: float 6s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0px) rotate(0deg); }
  50% { transform: translateY(-20px) rotate(180deg); }
}

.page-header h1 {
  margin: 0 0 10px 0;
  font-size: 32px;
  color: white;
  font-weight: 700;
  text-shadow: 0 2px 4px rgba(0,0,0,0.3);
  position: relative;
  z-index: 1;
}

.breadcrumb {
  color: rgba(255, 255, 255, 0.9);
  font-size: 16px;
  font-weight: 500;
  position: relative;
  z-index: 1;
}

.search-section {
  background: rgba(255, 255, 255, 0.95);
  padding: 25px 30px;
  border-radius: 16px;
  margin-bottom: 25px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  position: relative;
  overflow: hidden;
}

.search-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
}

.search-form {
  display: flex;
  gap: 20px;
  align-items: flex-end;
  flex-wrap: wrap;
}

.form-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-item label {
  font-weight: 500;
  color: #333;
  font-size: 14px;
}

.form-item input,
.form-item select {
  padding: 12px 16px;
  border: 2px solid #e1e5e9;
  border-radius: 12px;
  font-size: 14px;
  min-width: 180px;
  background: rgba(255, 255, 255, 0.9);
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.form-item input:focus,
.form-item select:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(102, 126, 234, 0.2);
}

.form-item input:hover,
.form-item select:hover {
  border-color: #adb5bd;
  transform: translateY(-1px);
}

.form-buttons {
  display: flex;
  gap: 10px;
}

.table-pagination-section {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  position: relative;
  overflow: hidden;
  margin-bottom: 25px;
}

.table-pagination-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #4facfe 0%, #00f2fe 50%, #43e97b 100%);
}

.table-container {
  overflow-x: auto;
  padding: 20px 20px 0 20px;
  background: transparent;
  border-radius: 0;
  box-shadow: none;
  border: none;
  backdrop-filter: none;
  position: static;
  overflow: visible;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.data-table th,
.data-table td {
  padding: 12px 8px;
  text-align: left;
  border-bottom: 1px solid #eee;
}

.data-table th {
  background: #f8f9fa;
  font-weight: 600;
  color: #495057;
  padding: 16px 12px;
  border: none;
  position: relative;
  border-bottom: 2px solid #e9ecef;
}

.data-table tbody tr {
  transition: all 0.3s ease;
  border-bottom: 1px solid rgba(225, 229, 233, 0.5);
}

.data-table tbody tr:hover {
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.05) 0%, rgba(118, 75, 162, 0.05) 100%);
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
}

.data-table tbody tr:last-child {
  border-bottom: none;
}

.pagination {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 20px 20px 20px;
  background: transparent;
  border-radius: 0;
  box-shadow: none;
  border: none;
  backdrop-filter: none;
  position: static;
  overflow: visible;
  border-top: 1px solid #e9ecef;
}

.pagination-info {
  color: #666;
  font-size: 14px;
}

.pagination-controls {
  display: flex;
  gap: 5px;
}

.pagination-jump {
  display: flex;
  align-items: center;
  gap: 8px;
}

.pagination-jump input {
  width: 80px;
  padding: 8px 12px;
  border: 2px solid #e1e5e9;
  border-radius: 8px;
  text-align: center;
  font-size: 14px;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.pagination-jump input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.btn {
  padding: 8px 16px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
  transition: all 0.3s ease;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.btn-primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
  color: white !important;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4) !important;
}

.btn-primary:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
}

.btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.btn-secondary {
  background: linear-gradient(135deg, #6c757d 0%, #495057 100%) !important;
  color: white !important;
  box-shadow: 0 4px 15px rgba(108, 117, 125, 0.4) !important;
}

.btn-secondary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(108, 117, 125, 0.6);
}

.btn-page {
  background: rgba(255, 255, 255, 0.9);
  color: #333;
  border: 2px solid #e1e5e9;
  border-radius: 12px;
  padding: 10px 16px;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.btn-page:hover {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-color: transparent;
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(102, 126, 234, 0.3);
}

.btn-page.active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-color: transparent;
  box-shadow: 0 4px 16px rgba(102, 126, 234, 0.4);
}

.btn-page:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-small {
  padding: 4px 8px;
  font-size: 12px;
}

.btn-icon {
  margin-right: 4px;
}

/* 模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  border-radius: 12px;
  width: 90%;
  max-width: 600px;
  max-height: 80vh;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  border: 1px solid #e1e5e9;
  position: relative;
}

.modal-header {
  padding: 18px 20px 16px 20px;
  border-bottom: 1px solid #e1e5e9;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 12px 12px 0 0;
}

.modal-header h3 {
  margin: 0;
  color: white;
  font-size: 16px;
  font-weight: 600;
}

.modal-close {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  font-size: 16px;
  cursor: pointer;
  color: white;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.modal-close:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(1.1);
}

.modal-body {
  padding: 16px 20px;
  max-height: calc(80vh - 120px);
  overflow-y: auto;
  overflow-x: hidden;
  width: 100%;
  box-sizing: border-box;
}

.modal-footer {
  padding: 14px 20px 18px 20px;
  border-top: 1px solid #e1e5e9;
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  background: #f8f9fa;
  border-radius: 0 0 12px 12px;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 16px;
}

.detail-item {
  display: flex;
  align-items: center;
  padding: 12px;
  background: #f8f9fa;
  border-radius: 8px;
  border-left: 4px solid #667eea;
}

.detail-item.full-width {
  grid-column: 1 / -1;
}

.detail-item label {
  font-weight: 500;
  color: #495057;
  margin-right: 12px;
  min-width: 100px;
}

.detail-item span {
  font-weight: 600;
  color: #2c3e50;
}

.highlight-amount {
  color: #e74c3c;
  font-weight: 700;
  font-size: 16px;
}
</style>


